﻿using LCA.Community.DAL;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Microsoft.WindowsAzure.StorageClient;
using LCA.Community.DAL.Model;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Practices.Unity;
using LCA.Community.DAL.Test.ModelFactories;
using LCA.Community.DAL.Interfaces;

namespace LCA.Community.DAL.Test
{
    /// <summary>
    ///This is a test class for BaseRepositoryTest and is intended
    ///to contain all BaseRepositoryTest Unit Tests
    ///</summary>
    [TestClass()]
    public class BaseRepositoryTest : BaseTestClass
    {
        #region Fields
        private List<User> _users;
        private UsersFactory _usersFactory = new UsersFactory();
        #endregion

        /// <summary>
        /// Initializes the test.
        /// </summary>
        [TestInitialize()]
        public void InitializeTest()
        {
            UnityContainerInitializer();

            var usersRepository = _unityContainer.Resolve<IBaseRepository<User>>();
            _users = _usersFactory.CreateList(3);
            _users.ForEach(user => usersRepository.Add(user));
        }

        /// <summary>
        /// Cleans up the test.
        /// </summary>
        [TestCleanup()]
        public void CleanupTest()
        {
            var usersRepository = _unityContainer.Resolve<IBaseRepository<User>>();
            usersRepository.DeleteTestEntities();
        }

        /// <summary>
        ///A test for Add
        ///</summary>
        [TestMethod()]
        public void AddTest()
        {
            var target = _unityContainer.Resolve<IBaseRepository<User>>();
            var entity = _usersFactory.CreateEntity();
            target.Add(entity);
            var actual = target.Get(entity.RowKey);

            Assert.AreEqual(actual, entity);
        }

        /// <summary>
        ///A test for Delete
        ///</summary>
        [TestMethod()]
        public void DeleteFromEntityTest()
        {
            var target = _unityContainer.Resolve<IBaseRepository<User>>();
            var entity = _users[0];
            target.Delete(entity);
            var actual = target.Get(entity.RowKey);

            Assert.IsNull(actual);
        }

        /// <summary>
        ///A test for Delete
        ///</summary>
        [TestMethod()]
        public void DeleteFromStringTest()
        {
            var target = _unityContainer.Resolve<IBaseRepository<User>>();
            string id = _users[1].RowKey;
            target.Delete(id);

            var actual = target.Get(id);
            Assert.IsNull(actual);
        }

        /// <summary>
        ///A test for Get
        ///</summary>
        [TestMethod()]
        public void GetTest()
        {
            var target = _unityContainer.Resolve<IBaseRepository<User>>();
            var id = _users[2].RowKey;
            var expected = _users[2];
            User actual;
            actual = target.Get(id);

            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for Update
        ///</summary>
        [TestMethod()]
        public void UpdateTest()
        {
            var target = _unityContainer.Resolve<IBaseRepository<User>>();
            var entity = _users[2];
            entity.FirstName = "Name3";
            target.Update(entity);
            var actual = target.Get(entity.RowKey);

            Assert.IsTrue(!String.IsNullOrEmpty(actual.FirstName));
        }
    }
}
